define(function (require, exports, module) {
    exports.ready = function () {
        require.async('$', function () {
            $(function () {
                var store_id = $('#store_id').val(),remark,address_id,delivery_date,delivery_time,goods_amount;

                /*判断文字数量*/
                $(document).on('input propertychange','#remark', function () {
                    remark = $(this).val();
                    var num = Number(remark.length), total = Number($('#total').html());
                    if (num <= total) {
                        $('#rest').html(num);
                    } else {
                        $('#rest').html(total);
                        remark = remark.substring(0, total);
                        $(this).val(remark);
                        fm.alert('输入的文字不能超过200个哟', 'background: #fff;color: #2a2a2a;');
                    }
                });

                //点击选址
                $(document).on('click', '.address', function () {
                    address_id = $(this).data('id');
                    $(this).addClass('address-check').siblings().removeClass('address-check');
                });

                $.ajax({
                    url: '/restaurant/TrustworthyShop/sendShow',
                    type: 'post',
                    data: {store_id: store_id},
                    dataType: 'json',
                    beforeSend: function () {
                        $('.circle-loadding').show();
                    },
                    success: function (data) {
                        if(data.status == 'S'){
                            var address = '',html = '',time = '';
                            $('.tips').html(data.cartCount).removeClass('hide');
                            $('#goods_total').html(data.sendData.length);
                            $('#total_price').html(Number(data.sumPrice).toFixed(2));

                            $.each(data.sendData,function(i,k){
                                html += '<div class="gc-flex gc-flex-lc gc-block-line" data-goods_id="'+ k.goods_id +'" data-goods_num="'+ k.goods_num +'">' +
                                    '<div class="item-img"><img src="'+ k.goods_thumb +'"></div><div class="item-msg gc-flex-item">' +
                                    '<div class="item-name gc-ft-normal">'+ k.goods_name +'</div>' +
                                    '<div class="gc-flex gc-flex-between"><span class="gc-ft-red price">￥'+ k.goods_price +'</span>' +
                                    '<span class="gc-ft-deepgray">X'+ k.goods_num +'</span></div></div></div>';
                            })
                            $('#goods').html(html);

                            $.each(data.areaData,function(i,k){
                                address += '<div class="gc-flex gc-flex-lc gc-block-line address" data-id="'+ k.area_id +'"><span class="iconfont icon-didian"></span>'+ k.name +'</div>'
                            })
                            $('#address').html(address);

                            for(k in data.delivery_time){
                                time += '<div class="day-item" data-id="'+k+'">'+data.delivery_time[k]+'</div>'
                            }
                            console.log(time);
                            $('.cart-info .main').html(time).find('.day-item').eq(0).trigger('click');
                        }
                        $('.circle-loadding').hide();
                    }
                })

                //点击获取日期
                $(document).on('click', '.day-item', function(){
                   $(this).addClass('active').siblings().removeClass('active');
                   var week = $(this).attr('data-id');

                   $.ajax({
                       url:'/restaurant/TrustworthyShop/getDateByWeek',
                       type:'post',
                       data:{
                           store_id:store_id,
                           week:week
                       },
                       dataType:'json',
                       success:function(data){
                           if(data.status === 'S'){
                               $('#date').text(data.date);
                           }
                       }
                   });
                });

                /*下单*/
                $(document).on('click','#doOrder',function(){
                    delivery_date = $('.day-item.active').attr('data-id');
                    delivery_time = $('#date').text();
                    goods_amount = parseFloat($('#total_price').text());
                    console.log(delivery_date, delivery_time);

                    var order_mark = [];
                    $('#goods .gc-block-line').each(function(){
                        order_mark.push({goods_id:$(this).data('goods_id'),goods_num:$(this).data('goods_num')});
                    });
                    if(!address_id){
                        fm.alert('请选择取货地址！','background:#fff;color:#2a2a2a');
                    }else {
                        $.ajax({
                            url: '/restaurant/TrustworthyShop/order',
                            type: 'post',
                            data: {
                                store_id: store_id,
                                order_mark: order_mark,
                                //remark: remark,
                                address_id:address_id,
                                delivery_time:delivery_time,
                                delivery_date:delivery_date,
                                goods_amount:goods_amount
                            },
                            dataType: 'json',
                            beforeSend: function () {
                                $('.circle-loadding').show();
                            },
                            success: function (data) {
                                if (data.status == 'S') {
                                    fm.alert(data.message, 'background:#fff;color:#2a2a2a');

                                    //location.href = data.url;

                                } else {
                                    fm.alert(data.message, 'background:#fff;color:#2a2a2a');
                                }
                                $('.circle-loadding').hide();
                            }
                        })
                    }
                })
            });
        });
    };
});
